<template>
  <div class="home" v-if="category" >
    <nav-bar ></nav-bar>
    <div class="categorytab search">
      <van-tabs v-model="active" swipeable sticky animated >
        
        <van-tab v-for="(item,index) in category" :key="index" :title="item.name" >
    
    <van-list
      v-model="item.loading"
      :immediate-check="false"
      :finished="item.finished"
      finished-text="已经到底了，去上传更多视频吧！"
      @load="onLoad"
    >
      <div class="detailparent">
        <cover
          class="detailitem"
          :detailitem="categoryitem"
          v-for="(categoryitem,categoryindex) in item.list"
          :key="categoryindex"
        />
      </div>
    </van-list>
  </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import cover from "@/components/common/cover.vue"
import NavBar from "@/components/common/Navbar.vue";
export default {
  data() {
    return {
      category: [],
      active: 0 ,
      flag:true ,
      content:''
    };
  },
  components: {
    cover ,
    NavBar
  },
  methods: {
    // this.$route.query.content
    async selectCategory() {
      const {data:res} = await this.$http.get("/tags")
 
      this.category = this.changeCategory(res.data);
      this.getsByLikes();
    },
    // 扩展数据
    changeCategory(data) {    
      const category1 = data.map((item, index) => {
        item.list = [];
        item.page = 0;
        item.finished = false;
        item.loading = true;
        item.pagesize = 12;
        return item;
      });
      return category1;
    },


    // 模糊查询
    async getsByLikes() {
      const categoryitem = this.categoryItem();
      const {data:res} = await this.$http.post("/videos/liket" , 
       {
          tid:categoryitem.id,
          page: categoryitem.page,
          size: categoryitem.pagesize ,
          likes : this.$route.query.content
        }
      );
      
      categoryitem.list.push(...res.data);
      categoryitem.loading = false;
      if (res.data.length < categoryitem.pagesize) {
        categoryitem.finished = true;
      }
    },
    onLoad() {
      const categoryitem = this.categoryItem();
      setTimeout(() => {
        categoryitem.page += 1;
        this.getsByLikes();
      }, 1000);
    },

    // 得到对应下标的 item
    categoryItem() {
      const categoryitem = this.category[this.active];
      return categoryitem;
    } ,

  },
  
  watch: {
    active() {
      const categoryitem = this.categoryItem();
      if (!categoryitem.list.length) {
        this.getsByLikes();
      }
    } ,
  },
  created() {
    this.selectCategory();
  }
};
</script>

<style lang="less" scoped>
.search {
  padding-top: 2vw;
 
}

.detailparent {
  width: 90%;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start !important;

  margin-left: 10vw;
  .detailitem {
    margin: 1vw 0;
    height: 13vw ;
    width: 16vw ;
    margin-left: 3vw;
  }
}


</style>